package user

import (
	"db/models"
	"encoding/json"
	"github.com/gorilla/mux"
	"net/http"
	"strconv"
)

//UserAll 返回全部用户
func (env *Env) UserAll(w http.ResponseWriter, r *http.Request) {
	limit, err := strconv.Atoi(r.FormValue("limit"))
	if limit == 0 {
		limit = 10
	}
	start, err := strconv.Atoi(r.FormValue("start"))
	users,count, err := env.db.GetAllUsers(limit, start)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	_ = json.NewEncoder(w).Encode(map[string]interface{}{"url": r.URL.Path, "count": count, "result": users})
	//这边有个地方值得注意，template.New()函数中参数名字要和ParseFiles（）
	//函数的文件名要相同，要不然就会报错："" is an incomplete template
	//tmpl := template.New("users.html").Funcs(template.FuncMap{"showtime": ShowTime})
	//tmpl, _ = tmpl.ParseFiles("template/users.html")

	//_ = t.Execute(w,  map[string]interface{}{"Users":users}) //执行模板的merger操作
	//迭代方式 {{range $i, $v := .Users}}
	//_ = tmpl.Execute(w,  users) //执行模板的merger操作
	//迭代方式{{range $i, $v := .}}
	return
}

//UserInfo 获取用户附加信息
func (env *Env) UserInfo(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := strconv.Atoi(vars["id"])
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	uis, err := env.db.GetUserInfo(int64(id), -1, 0)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	_ = json.NewEncoder(w).Encode(uis)
	return
}

func (env *Env) DeleteUser(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := strconv.ParseInt(vars["id"], 0, 64)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	count, err := env.db.DeleteUser(id)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	_ = json.NewEncoder(w).Encode(map[string]interface{}{"url": r.URL.Path, "count": count})
	return
}


func (env *Env) PutUser(w http.ResponseWriter, r *http.Request) {
	var user models.User
	if r.Body == nil {
		http.Error(w, "Please send a request body", 400)
		return
	}
	err := json.NewDecoder(r.Body).Decode(&user)
	count, err := env.db.PutUser(user)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	_ = json.NewEncoder(w).Encode(map[string]interface{}{"url": r.URL.Path, "count": count})
	return

}
func (env *Env) UpdateUser(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	id, err := strconv.ParseInt(vars["id"], 0, 64)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	var user models.User
	if r.Body == nil {
		http.Error(w, "Please send a request body", 400)
		return
	}
	err = json.NewDecoder(r.Body).Decode(&user)
	count, err := env.db.UpdateUser(id,user)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	_ = json.NewEncoder(w).Encode(map[string]interface{}{"url": r.URL.Path, "count": count})
	return
}

//User 获取用户信息
func (env *Env) User(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	id, err := strconv.ParseInt(vars["id"], 0, 64)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}

	ui,find, err := env.db.GetUserByID(id)
	if err != nil {
		http.Error(w, err.Error(), 500)
		return
	}
	if !find{
		http.Error(w,"User No Find", 404)
		return
	}
	_ = json.NewEncoder(w).Encode(ui)
	return
}